from sqlalchemy import Column, Integer, String, JSON

from models.base import Base
from models.base import db


class DocumentDom(Base):
    __tablename__ = 'document_dom'
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_number = Column(String(30), nullable=False)
    node = Column(String(30))
    attrs = Column(JSON)
    index = Column(Integer)
    level = Column(Integer)
    text = Column(String(2000))
    path = Column(String(500))
    parent = Column(String(30))

    def insert_doms(self, params):
        db.session.bulk_insert_mappings(DocumentDom, params)
        db.session.commit()

    def HtmlDoms(self, order_number, level):
        doms = db.query(DocumentDom).filter_by(order_number=order_number).filter_by(level=level).filter_by().order_by(DocumentDom.id).all()
        return doms

    def DomText(self, order_number):
        doms = db.query(DocumentDom).filter_by(order_number=order_number).filter(DocumentDom.text != None).order_by(DocumentDom.id).all()
        return doms
